package emergence.game.gfx;

public class ColoredSprite implements Renderable {

    private int color;
    private int[][] pixels;
    private final int width;
    private final int height;
    private final Sprite sprite;

    public ColoredSprite(Sprite sprite, int color) {
        this.sprite = sprite;
        this.color = color;
        this.width = sprite.width();
        this.height = sprite.height();

        pixels = new int[sprite.width()][sprite.height()];
        for (int i = 0; i < pixels.length; i++) {
            for (int j = 0; j < pixels[0].length; j++) {
                this.pixels[i][j] = ((color >> sprite.pixels()[i][j] * 8)) & 255;
            }
        }
    }

    public Sprite sprite() {
        return sprite;
    }

    public int color() {
        return color;
    }

    @Override
    public int[][] pixels() {
        return pixels;
    }

    public int width() {
        return width;
    }

    public int height() {
        return height;
    }

    public void setColors(int newColor) {
        this.color = newColor;
        pixels = new int[sprite.width()][sprite.height()];
        for (int i = 0; i < pixels.length; i++) {
            for (int j = 0; j < pixels[0].length; j++) {
                this.pixels[i][j] = ((newColor >> sprite.pixels()[i][j] * 8)) & 255;
            }
        }
    }
    
    public ColoredSprite flip(boolean flipX, boolean flipY) {
        return new ColoredSprite(sprite.flip(flipX, flipY), color);
    }
}
